﻿#region 命名空间

using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using WinnerCMS.Data;
using WinnerCMS.Common;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL
{
    /// <summary>
    /// VoteManage 的摘要说明。
    /// </summary>
    public partial class VoteManage : MarshalByRefObject
    {
        private static readonly VoteDal Dal = new VoteDal();

        #region 保存投票

        /// <summary>
        /// 保存投票
        /// </summary>
        /// <param name="model"></param>
        /// <param name="ids"></param>
        /// <param name="choiceTitles"></param>
        /// <param name="voteNums"></param>
        /// <returns></returns>
        [Logging, ExceptionCall]
        public Result Save(Vote model, string ids, string choiceTitles, string voteNums)
        {
            Result resu = new Result();
            if (model.Title.Length == 0)
            {
                resu.Errors = "“投票主题”不能为空！";
                return resu;
            }

            //效验数据
            string[] arrId = ids.Split(',');
            string[] arrTitle = choiceTitles.Split(',');
            string[] arrVoteNum = voteNums.Split(',');

            int iVoteid;
            if (model.Id == 0)
            {
                iVoteid = Dal.Add(model);
                resu.LogMessage = "添加投票" + model.Title;
            }
            else
            {
                Dal.Edit(model);
                resu.LogMessage = "修改投票" + model.Id;
                iVoteid = model.Id;
            }

            #region 添加修改投票项目

            VoteChoiceManage vc = new VoteChoiceManage();
            for (int i = 0; i < arrTitle.Length; i++)
            {
                if (arrTitle[i].Length == 0)
                {
                    resu.Errors = "“选项”不能为空！";
                    continue;
                }

                VoteChoice choiceModel = new VoteChoice
                {
                    Id = int.Parse(arrId[i]),
                    ChoiceTitle = arrTitle[i]
                };

                if (MyValidate.IsNumeric(arrVoteNum[i]) == false)
                {
                    choiceModel.VoteNum = 0;
                }
                else
                {
                    choiceModel.VoteNum = int.Parse(arrVoteNum[i]);
                }

                choiceModel.VoteId = iVoteid;


                vc.Add(choiceModel);
            }

            #endregion

            return resu;
        }

        #endregion

        /// <summary>
        /// 删除投票
        /// </summary>
        /// <param name="id"></param>
        [Logging("删除投票，Id：{@p0.ID}"), ExceptionCall]
        public Result Delete(string id)
        {
            Result res = new Result();
            Dal.Delete(int.Parse(id));
            return res;
        }

        /// <summary>
        /// 加载投票
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Vote GetInfo(int id)
        {
            return Dal.GetInfo(id);
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="id"></param>
        [Logging("通过投票审核，Id：{@p0}"), ExceptionCall]
        public Result Pass(string id)
        {
            Result res = new Result();
            try
            {
                Dal.Pass(int.Parse(id));
            }
            catch (Exception ex)
            {
                res.Errors = ex.Message;
            }

            return res;
        }

        /// <summary>
        /// 绑定
        /// </summary>
        /// <returns></returns>
        public IList<Vote> BindList(PagingParam pp)
        {
            return Dal.BindList(pp);
        }
    }
}